﻿



CREATE PROCEDURE [dbo].[amix_StergeUnitate] 
 @CodUnitate smallint AS
BEGIN
DECLARE @NrDoc AS int
SET @NrDoc = 0

BEGIN TRAN
PRINT('[AmixSetup]')
DELETE FROM [AmixSetup] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 
PRINT('[AmixAmortizare]')
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AmixAmortizare]') AND type in (N'U'))
	DELETE FROM [AmixAmortizare] WHERE IDMijlocFix in (SELECT IDMijlocFix FROM [AmixRegistru] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 
PRINT('[AmixAnsamble]')
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AmixAnsamble]') AND type in (N'U'))
	DELETE FROM [AmixAnsamble] WHERE IDMFixPrincipal in (SELECT IDMijlocFix FROM [AmixRegistru] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 
PRINT('[AmixComponente] - nu se sterge(view)')
  
PRINT('[AmixComponenteSecundare]')
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AmixComponenteSecundare]') AND type in (N'U'))
DELETE FROM [AmixComponenteSecundare] WHERE IDMijlocFix in (SELECT IDMijlocFix FROM [AmixRegistru] WHERE [CodUnitate]=@CodUnitate)
IF @@ERROR <>0
    GOTO Eroare 
   
PRINT('[AmixDocumente]')
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AmixDocumente]') AND type in (N'U'))
DELETE FROM [AmixDocumente] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare       
      
PRINT('[AmixRegistru]')
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AmixRegistru]') AND type in (N'U'))
DELETE FROM [AmixRegistru] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 
    
PRINT('[AmixRegistruBK]')
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AmixRegistruBK]') AND type in (N'U'))
DELETE FROM [AmixRegistruBK] WHERE [CodUnitate]=@CodUnitate
IF @@ERROR <>0
    GOTO Eroare 

PRINT('nu se sterg [AmixGestionari]')


COMMIT TRAN    
GOTO Gata
Eroare:
  ROLLBACK
  RAISERROR ('Eroare la executia procedurii StergeUnitate',-1,0)
Exista_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine documente!',-1,0)
Exista_Rulaje_Documente:
  ROLLBACK
  RAISERROR ('Unitatea nu poate fi stearsa deoarece contine rulaje - documente!',-1,0)

Gata:

END